package com.ciban.smarthome.module.engine;

import android.content.Context;

import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.Constants;
import com.ciban.smarthome.module.db.dao.ProjectorDao;
import com.ciban.smarthome.module.engine.base.BaseEngine;
import com.ciban.smarthome.module.entity.Equip_Projector;
import com.ciban.smarthome.module.entity.Host;
import com.ciban.smarthome.module.net.communication.Communicate;
import com.ciban.smarthome.module.net.communication.ProtocolHelper;

import java.util.ArrayList;

/**
 * Created by wdl on 2015/11/10.
 */
public class PorjectorEngine extends BaseEngine {


    private Host host;
    private ProjectorDao dao;
    private String area_id;
    private int type; //设备的类型
    private ArrayList<Equip_Projector> proList;

    public PorjectorEngine(Context context) {
        super(context);
    }

    public PorjectorEngine(Context context, String area_id, int type) {
        super(context);
        this.area_id = area_id;
        this.type = type;
        host = getHostInfo(area_id);
    }

    public ArrayList<Equip_Projector> getProList() {
        if (proList == null)
            proList = getProListFromDb();
        return proList;
    }

    public void refreshProList() {
        proList.clear();
        proList.addAll(getProListFromDb());
    }

    private ArrayList<Equip_Projector> getProListFromDb() {
        checkDao();
        return dao.getProjectorList(area_id, type);
    }

    public boolean delProjector(Equip_Projector info) {
        checkDao();
        return dao.delProjector(info.getId()) > 0;
    }

    /**
     * 添加新的投影仪信息（基本信息+投影幕）
     *
     * @param info
     * @return
     */
    public boolean addPorjector(Equip_Projector info) {
        checkDao();
        LogUtil.info("添加新的投影仪：" + info.toString());
        String id = addNewEquipMent(info);
        if (StringUtils.isEmpty(id))
            return false;
        info.setId(id + "");
        return dao.addNewProjector(info) > 0;
    }

    /**
     * 更新投影仪信息
     *
     * @param info
     * @return
     */
    public boolean updateProjector(Equip_Projector info) {
        checkDao();
        boolean isup = upEquipment(info) > 0;
        return isup | dao.updateProjector(info) > 0;
    }

    public void sendCmd(int cmdType, Equip_Projector info, Communicate.ConnListener listener) {
        String cmd = null;
        switch (cmdType) {
            case Constants.Projector.KEYCODE_POWER_ON:
            case Constants.Projector.KEYCODE_POWER_OFF:
            case Constants.Projector.KEYCODE_HDMI:
            case Constants.Projector.KEYCODE_PC:
            case Constants.Projector.KEYCODE_S_ViDEO:
            case Constants.Projector.KEYCODE_VIDEO:
                cmd = ProtocolHelper.getIfCmd(host,
                        info.getSerial_no(), cmdType);
                break;
            default:
                cmd = ProtocolHelper.getNormalCmd(
                        host, info.getPro_screen_serialno(), cmdType);
                break;
        }
        LogUtil.info("投影仪相关命令（" + cmdType + "):" + cmd);
        sendData(cmd, host, listener);
    }

    private void checkDao() {
        if (dao == null)
            dao = new ProjectorDao(context);
    }
}
